<%--
  Created by IntelliJ IDEA.
  User: Cc
  Date: 2020/12/30
  Time: 16:51
  To change this template use File | Settings | File Templates.
--%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" +
            request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script type="text/javascript" src="/static/js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
</head>

<body>
<div class="x-body">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="gName" class="layui-form-label">
                <span class="x-red">*</span>商品名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="gName" name="gName"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red"></span>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="gNo" class="layui-form-label">
                商品编号
            </label>
            <div class="layui-input-inline">
                <input type="text" id="gNo" name="gNo" onblur="findGoodsGNno();"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span id="gMsg" class="x-red"></span>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">上架人员</label>
            <div class="layui-input-block">
                <select id="uName" name="uName" lay-verify="required">
                    <option value=""></option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="gCount" class="layui-form-label">
                上架数量
            </label>
            <div class="layui-input-inline">
                <input type="tel" id="gCount" name="gCount"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                上架时间
            </label>
            <div class="layui-inline"> <!-- 注意：这一层元素并不是必须的 -->
                <input type="text" class="layui-input" id="gDate" placeholder="yyyy-MM-dd HH:mm:ss"  autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">型号</label>
            <div class="layui-input-block">
                <select id="mType" name="mType" lay-verify="required">
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">规格</label>
            <div class="layui-input-block">
                <select id="sNorms" name="sNorms" lay-verify="required">
                    <option value=""></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
            </label>
            <button type="button"  class="layui-btn" id="L_repass" onclick="insertGoods();">
                增加
            </button>
        </div>
    </form>
</div>
<script>
    layui.use('laydate', function(){
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#gDate'
            ,trigger: 'click'
            ,type: 'datetime'
        });
    });
    $(function () {
        $("#uName").html(localStorage.getItem("uName"));
        $("#mType").html(localStorage.getItem("mType"));
        $("#sNorms").html(localStorage.getItem("sNorms"));
    })
    function insertGoods() {
        if($("#gName").val().trim()==""||$("#gNo").val().trim()==""){
            layer.alert("商品名和商品编号不许为空！",{icon:5})
        }else{
            $.ajax({
                url:"../goods/insertGoods",
                type:"post",
                async:false,
                data:{
                    "gName":$("#gName").val(),
                    "gNo":$("#gNo").val(),
                    "uAccount":$("#uName option:checked").val(),
                    "gCount":$("#gCount").val(),
                    "gDate":$("#gDate").val(),
                    "mType":$("#mType option:checked").val(),
                    "sNorms":$("#sNorms option:checked").val(),
                },
                dataType:"json",
                success:function (data) {
                    if (data.code == 200){
                        layer.alert("添加成功",{icon:6},function(){
                            // 获得frame索引
                            let index = parent.layer.getFrameIndex(window.name);
                            //刷新父页面
                            parent.location.reload();
                            //关闭当前frame
                            parent.layer.close(index);
                        })
                    }else {
                        layer.alert("更新失败",{icon:5})
                    }
                },error:function () {
                    layer.alert("服务器错误",{icon:7});
                }
            })
        }
    }
    function findGoodsGNno() {
        $.ajax({
            url:"../goods/findGoodsGNno",
            type:"GET",
            data:{
                "gNo":$("#gNo").val(),
            },
            dataType:"json",
            success:function (data) {
                if (data.code == 200){
                    layer.alert(data.msg,{icon:5});
                    $("#gMsg").html("该编号已存在");
                    $("#L_repass").addClass("layui-btn-disabled");
                    $("#L_repass").attr("disabled",true);
                }else{
                    $("#gMsg").html("");
                    $("#L_repass").attr("disabled",false);
                    $("#L_repass").removeClass("layui-btn-disabled");
                }
            }
        })
    }
</script>
</body>
</html>